package dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import modelDB.TempCustomer;

public class TempCustomerDAO implements Manageable<TempCustomer, Integer>{

	private EntityManager entityManager;

	public TempCustomerDAO() {
		entityManager = DatabaseConnector.getEntityManager();
	}
	
	@Override
	public boolean insert(TempCustomer obj) {
		try {
			entityManager.getTransaction().begin();
			entityManager.persist(obj);
			entityManager.getTransaction().commit();
			
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean update(TempCustomer tempCus) {
		try {
			entityManager.getTransaction().begin();
			
			TempCustomer tempCustomer = entityManager.find(TempCustomer.class, tempCus.getId());
			if(tempCustomer != null) {
				tempCustomer.setLastingTime(tempCus.getLastingTime());
			}
			
			entityManager.getTransaction().commit();
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean delete(Integer id) {
		try {
			entityManager.getTransaction().begin();
			
			TempCustomer tempCustomer = entityManager.find(TempCustomer.class, id);
			if (tempCustomer != null) {
				entityManager.remove(tempCustomer);
			}
			
			entityManager.getTransaction().commit();
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public List<TempCustomer> selectAll() {
		Query query = entityManager.createQuery("SELECT t FROM TempCustomer t");
		return query.getResultList();
	}

	@Override
	public TempCustomer findByID(Integer id) {
		return entityManager.find(TempCustomer.class, id);
	}

}
